package itqf.dao;


import itqf.dao.impl.CartDao;
import itqf.entity.Cart;
import itqf.entity.Product;
import itqf.utils.DruidUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * com.itqf.dao
 * 2020/9/16
 */
public class CartDaoImpl implements CartDao {
    QueryRunner qr = new QueryRunner(DruidUtils.getDateSource());
    @Override
    public Cart findCartByUidAndPid(int uid, int pid) {
        String sql = "select c_id as cid,u_id as uid,p_id as pid,c_count as ccount, c_num as cnum from cart where u_id=? and p_id=?";
        try {
            return qr.query(sql,new BeanHandler<>(Cart.class),uid,pid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int update(Cart cart) {
        String sql = "update cart set c_num=? ,c_count=? where c_id=?";
        try {
            return qr.update(sql,cart.getCnum(),cart.getCcount(),cart.getCid());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int addCart(Cart cart) {
        String sql = "insert into cart(c_id,u_id,p_id,c_count,c_num) values(?,?,?,?,?)";
        try {
            return qr.update(sql,cart.getCid(),cart.getUid(),cart.getPid(),cart.getCcount(),cart.getCnum());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Cart> findAllCart(int uid) {
        String sql  ="select c.c_id as cid,c.p_id as pid,c.c_count as ccount,c.c_num as cnum," +
                "p_image as pimage,p.p_price as pprice,p.p_info as pinfo,p_name as pname from cart c inner join product p on c.p_id = p.p_id where c.u_id=?";
        List<Cart> carts = new ArrayList<>();
        try {
            List<Map<String,Object>> list = qr.query(sql,new MapListHandler(),uid);
            for (Map<String, Object> map : list) {
                Cart cart = new Cart();
                Product product = new Product();
                BeanUtils.populate(cart,map);
                BeanUtils.populate(product,map);
                cart.setProduct(product);
                carts.add(cart);
            }
            return  carts;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int  del(int uid, int pid) {
        String sql = null;
        try {
            if(pid==0){
                sql = "delete from cart where u_id=?";
                return qr.update(sql,uid);
            }else{
                sql = "delete from cart where u_id=? and p_id=?";
                return qr.update(sql,uid,pid);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
